package myqueue;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
class MyStack {
    public Deque<Integer> queue1;
    public Deque<Integer> queue2;
    public int size;
    public MyStack() {
        queue1 =new LinkedList<>();
        queue2 =new LinkedList<>();
    }

    public void push(int x) {
        if(queue1.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
        size++;
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        int data=0;
        if(queue1.isEmpty()){
            int num=queue2.size();
            for(int i=0;i<num-1;i++){
                queue1.offer(queue2.poll());
            }
            data= queue2.poll();
        }else{
            int num =queue1.size();
            for(int i=0;i<num-1;i++){
                queue2.offer(queue1.poll());
            }
            data= queue1.poll();
        }
        size--;
        return data;
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(queue1.isEmpty()){
            int value=0;
            int num =queue2.size();
            for(int i=0;i<num;i++){
                value =queue2.poll();
                queue1.offer(value);
            }
            return value;
        }else{
            int value=0;
            int num =queue1.size();
            for(int i=0;i<num;i++){
                value =queue1.poll();
                queue2.offer(value);
            }
            return value;
        }
    }

    public boolean empty() {
        return size==0;
    }
}
